/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean.TX.SABM;

import entity.tx.Advertisement;
import entity.tx.AdvertisementStatistics;
import entity.establishment.Stakeholder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import managedBean.TX.OutletManagement.PhotoMB;
import session.TX.AdvertisementSB;

/**
 *
 * @author AdminNUS
 */
@ManagedBean(name = "advertisementMB")
@SessionScoped
public class AdvertisementMB {

    @EJB
    private AdvertisementSB adsBean;
    @ManagedProperty(value = "#{photoManagedBean}")
    private PhotoMB photoMB;
    private String tags;
    private String filePath = "";
    private String description;
    private String link;
    private boolean isSubscription = false;
    private boolean isImpression;
    private boolean isClick;
    private Double biddingPrice;
    private boolean isActivated;
    private Double maxClicks;
    private Double maxImpression;
    private Stakeholder stakeholder;
    private List<AdvertisementStatistics> AdvertisementStatistics;
    private Date startDate;
    private Date endDate;
    private String statusMessage;
    private String title;
    private String stakeholderName;

    /**
     * @return the filePath
     */
    public String getFilePath() {
        return filePath;
    }

    /**
     * @param filePath the filePath to set
     */
    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    /**
     * @return the description
     */
    public String getDescription() {
        return description;
    }

    /**
     * @param description the description to set
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * @return the link
     */
    public String getLink() {
        return link;
    }

    /**
     * @param link the link to set
     */
    public void setLink(String link) {
        this.link = link;
    }

    /**
     * @return the isSubscription
     */
    public boolean isIsSubscription() {
        return isSubscription;
    }

    /**
     * @param isSubscription the isSubscription to set
     */
    public void setIsSubscription(boolean isSubscription) {
        this.isSubscription = isSubscription;
    }

    /**
     * @return the isImpression
     */
    public boolean isIsImpression() {
        return isImpression;
    }

    /**
     * @param isImpression the isImpression to set
     */
    public void setIsImpression(boolean isImpression) {
        this.isImpression = isImpression;
    }

    /**
     * @return the biddingPrice
     */
    public Double getBiddingPrice() {
        return biddingPrice;
    }

    /**
     * @param biddingPrice the biddingPrice to set
     */
    public void setBiddingPrice(Double biddingPrice) {
        this.biddingPrice = biddingPrice;
    }

    /**
     * @return the isActivated
     */
    public boolean isIsActivated() {
        return isActivated;
    }

    /**
     * @param isActivated the isActivated to set
     */
    public void setIsActivated(boolean isActivated) {
        this.isActivated = isActivated;
    }

    /**
     * @return the maxClicks
     */
    public Double getMaxClicks() {
        return maxClicks;
    }

    /**
     * @param maxClicks the maxClicks to set
     */
    public void setMaxClicks(Double maxClicks) {
        this.maxClicks = maxClicks;
    }

    /**
     * @return the maxImpression
     */
    public Double getMaxImpression() {
        return maxImpression;
    }

    /**
     * @param maxImpression the maxImpression to set
     */
    public void setMaxImpression(Double maxImpression) {
        this.maxImpression = maxImpression;
    }

    /**
     * @return the stakeholder
     */
    public Stakeholder getStakeholder() {
        return stakeholder;
    }

    /**
     * @param stakeholder the stakeholder to set
     */
    public void setStakeholder(Stakeholder stakeholder) {
        this.stakeholder = stakeholder;
    }

    /**
     * @return the AdvertisementStatistics
     */
    public List<AdvertisementStatistics> getAdvertisementStatistics() {
        return AdvertisementStatistics;
    }

    /**
     * @param AdvertisementStatistics the AdvertisementStatistics to set
     */
    public void setAdvertisementStatistics(List<AdvertisementStatistics> AdvertisementStatistics) {
        this.AdvertisementStatistics = AdvertisementStatistics;
    }

    /**
     * @return the tags
     */
    public String getTags() {
        return tags;
    }

    /**
     * @param tags the tags to set
     */
    public void setTags(String tags) {
        this.tags = tags;
    }

    public void addNewAdvertisement(ActionEvent event) {


        boolean status = adsBean.addAdvertisement(getTitle(),tags,
                photoMB.getPhotoFilePath(),
                description,
                link,
                isSubscription,
                isImpression, isClick,
                biddingPrice,
                isActivated,
                maxClicks,
                maxImpression, FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("stakeholderName").toString(), startDate, endDate);

        if (status == true) {
            setStatusMessage("Advertisement created successfully");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Result: " + getStatusMessage(), ""));
        } else {
            setStatusMessage("Advertisement not created");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Result: " + getStatusMessage(), ""));
        }
    }
    
    public List<Advertisement> retrieveAdvertisement(){
        String stakeholderName ="";
        stakeholderName = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("stakeholderName").toString();
        List<Advertisement> adList = new ArrayList<Advertisement>();
        adList = adsBean.retrieveAds(stakeholderName);
        return adList;
    }
    
    public List<Advertisement> retrieveAdvertisement2(){
        //String stakeholderName ="";
        //stakeholderName = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("stakeholderName").toString();
        List<Advertisement> adList = new ArrayList<Advertisement>();
        if(stakeholderName != null){
            
            adList = adsBean.retrieveAds2(stakeholderName);
            //if(adList==null)
                //return new ArrayList<Advertisement>();
            
            
            return adList;
        }else{
            return adList;
        }
    }
    
    public void deActivate(Advertisement a){
        long id = a.getId();
        adsBean.deActivate(id);
    }

    /**
     * @return the startDate
     */
    public Date getStartDate() {
        return startDate;
    }

    /**
     * @param startDate the startDate to set
     */
    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    /**
     * @return the endDate
     */
    public Date getEndDate() {
        return endDate;
    }

    /**
     * @param endDate the endDate to set
     */
    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    /**
     * @return the statusMessage
     */
    public String getStatusMessage() {
        return statusMessage;
    }

    /**
     * @param statusMessage the statusMessage to set
     */
    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    /**
     * @return the photoMB
     */
    public PhotoMB getPhotoMB(){
        if (photoMB == null) {
            FacesContext context = FacesContext.getCurrentInstance();     
            Application app = context.getApplication();     
            photoMB = (PhotoMB) app.evaluateExpressionGet(context, "#{photoManagedBean}", PhotoMB.class);
        }       
        return photoMB;
    }

    /**
     * @param photoMB the photoMB to set
     */
    public void setPhotoMB(PhotoMB photoMB) {
        this.photoMB = photoMB;
    }

    /**
     * @return the isClick
     */
    public boolean isIsClick() {
        return isClick;
    }

    /**
     * @param isClick the isClick to set
     */
    public void setIsClick(boolean isClick) {
        this.isClick = isClick;
    }

    /**
     * @return the title
     */
    public String getTitle() {
        return title;
    }

    /**
     * @param title the title to set
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     * @return the stakeholderName
     */
    public String getStakeholderName() {
        return stakeholderName;
    }

    /**
     * @param stakeholderName the stakeholderName to set
     */
    public void setStakeholderName(String stakeholderName) {
        this.stakeholderName = stakeholderName;
    }
}
